<?php
/**
 * Created by PhpStorm.
 * User: jason
 * Date: 2017/11/8
 * Time: 23:34
 */

namespace Home\Service;


use Home\Enums\MemberLevel;
use Think\Log;
use Think\Model;

class UserMemberService extends Model
{

    /**
     * 获取后台会员收费设定
     */
    public function coinCharge($uid)
    {
        //step-1: 获取用户帐户金币余额
        $userModel = M('UserCoinWallet');
        $userAccount = $userModel->where(array('user_id'=>$uid))->find();
        $data = array();
        if ($userAccount) {
            $data['account']['amount'] = $userAccount['amount'];
            $data['account']['member_days'] = $userAccount['member_days'];
        } else {
            $data['account']['amount'] = 0;
            $data['account']['member_days'] = 0;
        }

        //step-2: 获取收费设定
        $model = M('UserCoinCharge');
        $map = array(
            'is_delete'=>0
        );
        $chargeSet = $model->where($map)->field('id, name, amount, additional_coin, original_price, discount_price')->select();
        foreach ($chargeSet as $key => $val) {
            $chargeSet[$key]['member_days'] = $val['amount'] + $val['additional_coin'];

        }

        $data['charge_set'] = $chargeSet;
        return $data;
    }

    /**
     * 充值会员帐户金币
     * @param $uid  /用户id
     * @param $amount /充值金币数量
     * @param int $style  /乐器类型
     * @return mixed
     */
    public function rechargeUserAccount($uid, $amount, $style=1)
    {
        $model = M('UserCoinWallet');
        $map = array('user_id'=>$uid);
        $user = $model->where($map)->find();
        if (!empty($user)) {
            $data = array(
                'amount' => $user['amount'] + $amount,
                'update_time' => date('Y-m-d H:i:s', time())
            );
            $res = $model->where($map)->save($data);
        } else {
            $data = array(
                'user_id' => $uid,
                'amount' => $amount,
                'style' => $style,
            );
            $res = $model->add($data);
        }
        Log::writeInfo('Recharge coin. The'. $uid .'\'s original coin amount is '.$user['amount'].'.And now the coin amount is '.$data['amount'], 'recharge');
        return $res;
    }

    /**
     * 更新用户金币流水
     * @param $uid
     * @param $amount 充值为正，消费为负
     * @param $type 1充值，2消费
     * @param $time
     * @param $orderType 100-充值金币 ；200-消费会员 201-购买曲谱
     * @param $orderNo
     * @return mixed
     */
    public function updateUserPayment($uid, $amount, $type, $time, $orderType, $orderNo)
    {
        $model = M('UserWalletList');
        $data = array(
            'user_id' => $uid,
            'amount' => $amount,
            'type' => $type,
            'create_time' => $time,
            'order_type' => $orderType,
            'order_id' => $orderNo
        );
        $id = $model->add($data);
        return $id;
    }


    /**
     * 获取会员等级信息
     * @param $uid
     * @return array|bool
     */
    public function getMemberInfo($uid)
    {
        if (!$uid) {
            return false;
        }
        $member = M('UserCoinWallet')->where(['user_id'=>$uid])->find();
        if (!$member) {
            //非会员
            $memberInfo = [
                'next_level' => [],
                'member_level'=> '未知',
                'member_days' => 0,
                'sheet_click' => 0,
                'is_member' => false
            ];
        } else {
            if ($member['amount'] > 0) {
                //获取会员下一等级配置
                $config = M('UserMemberLevels')->where(['level'=> $member['level'] + 1])->find();
                $memberInfo = [
                    'member_level'=> MemberLevel::$style[$member['style']].MemberLevel::$level[$member['level']],
                    'member_days' => $member['member_days'],
                    'sheet_click' => $member['sheet_click'],
                    'is_member' => 1
                ];
                if ($member['level'] < 10 ) {
                    $memberInfo['next_level'] = [
                        'member_level'=> MemberLevel::$style[$member['style']].MemberLevel::$level[$member['level']+1],
                        'member_days' => $config['member_days'] - $member['member_days'],
                        'sheet_click' => $config['sheet_click'] - $member['sheet_click'],
                    ];
                }
            } else {
                //会员失效
                $memberInfo = [
                    'next_level' => [],
                    'member_level'=> MemberLevel::$style[$member['style']].MemberLevel::$level[$member['level']],
                    'member_days' => $member['member_days'],
                    'sheet_click' => $member['sheet_click'],
                    'is_member' => false
                ];
            }
        }
        return $memberInfo;
    }
}